<template>
  <div>
    <!-- 线索热度定义弹窗 -->
    <div>
      <FormPanel
        ref="formPanel"
        :list="formList"
        :cols="1"
        :formType="type"
        labelWidth="110"
        :isSubmitBtn="false"
        @formChange="formChangeHandle"
      />
    </div>
    <div
      :style="{
        position: 'absolute',
        left: 0,
        bottom: 0,
        right: 0,
        zIndex: 9,
        borderTop: '1px solid #e9e9e9',
        padding: '10px 20px',
        background: '#fff',
        textAlign: 'center'
      }"
    >
      <el-button @click="closeDialog">取 消</el-button>
      <AjaxButton :click="saveDialog" type="primary">确 定</AjaxButton>
    </div>
  </div>
</template>

<script>
import { dictionary } from '@/utils/dictMixin';
import {
  getIdParameter,
  addClueParameter,
  editClueParameter,
  queryClueSourceList,
  getAllHeatUp
} from '@/api/potenCus/basedata/clueFever';
import {
  getCategories,
  getPartitionById
} from '@/api/potenCus/basedata/cluesPrameter';
export default {
  name: 'ChannelModal2',
  mixins: [dictionary],
  props: {
    type: {
      type: String,
      default: 'add'
    },
    rowData: {
      type: Object,
      default: () => ({})
    }
  },
  data() {
    this.saveData = null;
    return {
      formList: this.createFormList(),
    };
  },
  created() {
    this.init();
    this.getClueTree()
    this.getAllHeatUp()
  },
  methods: {
    async init() {
      if (this.type === 'edit') {
        // 更新接口需要的记录 ID
        this.recordId = this.rowData.id;
        // this.rowData.isValid = 10041002;
        this.rowData.vehicleMark5 = [this.rowData.frequencyMin,this.rowData.frequencyMax];
        this.rowData.clueSourceBig = this.rowData.clueSourceBig ? Number(this.rowData.clueSourceBig):null;
        this.formList.forEach(x => {
          x.initialValue = this.rowData[x.fieldName];
        });

      }
    },
    createFormList() {
      return [
				{
          type: 'SELECT',
          label: '线索热度',
          fieldName: 'parentId',
          placeholder: '请输入',
          itemList: [],
          // rules: [{ required: true, message: "请输入线索热度", trigger: "blur"},
          // { min: 1, max: 32, message: "长度在1到32个字符", trigger: "blur"}],
          rules: [{ required: true, message: '请选择热度', trigger: 'blur' }]
        },
        {
          type: 'SELECT',
          label: '是否有效',
          fieldName: 'isValid',
          placeholder: '请选择',
          initialValue: '',
          style: { width: '250px' },
          itemList: this.createDictList('1004'),
          rules: [{ required: true, message: '请选择是否有效', trigger: 'blur' }]
        },
        {
          type: 'BREAK_SPACE',
          label: '评级规则(同时满足以下条件)',
        },
        {
          type: 'RANGE_INPUT_NUMBER',
          label: '线索出现频次',
          fieldName: 'vehicleMark5',
          initialValue: [0],
          style: { width: '150px' },
        },
        {
          type: 'INPUT_TREE',
          label: '线索来源',
          fieldName: 'clueSourceBig',
          placeholder: '全部',
          itemList: []
        },
        {
          type: 'SELECT',
          label: '线索类别',
          fieldName: 'clueType',
          placeholder: '全部',
          itemList: this.createDictList('7019'),
        },
      ];
    },
    formChangeHandle(val) {
      this.saveData = val;
    },
    async saveDialog (){ //确定
      // console.log('确定')
      this.$refs.formPanel.SUBMIT_FORM();
      // console.log(this.saveData)
      if (this.saveData !== null) {
        let res = {};
        this.saveData.level = 2;
        this.saveData.frequencyMin = this.saveData.vehicleMark5[0];
        this.saveData.frequencyMax = this.saveData.vehicleMark5[1] ? this.saveData.vehicleMark5[1]:undefined;
        if (this.type === 'add') {
          res = await addClueParameter(this.saveData);
        }
        if (this.type === 'edit') {
          res = await editClueParameter(this.recordId, {...this.saveData,id:this.recordId});
        }
        if (res.resultCode === 200) {
          this.$message.success("保存成功!");
          this.closeDialog('refresh');
        }
      }
    },
    closeDialog(param) { //取消
      this.$emit('close', false, param);
    },
    // 潜客来源
    async getClueTree() {
      const res = await queryClueSourceList();
      // console.log('潜客来源', res)
      if (res.resultCode === 200) {
        let treeList = res.data
        treeList.forEach(el => {
          el.text = el.clueName;
          el.value = el.id;
          el.parentId = el.parentId;
        });

        this.formList.find(item => item.fieldName ==='clueSourceBig').itemList = treeList
      }
    },
    // 获取所有线索热度级别定义
    async getAllHeatUp() {
      const res = await getAllHeatUp({isValid:10041001});
      // console.log('潜客来源', res)
      if (res.resultCode === 200) {
        let hotList = []
        res.data.forEach(el => {
          hotList.push({
            text:el.clueHeatName,
            value:el.id
          })
        });
        this.formList.find(item => item.fieldName ==='parentId').itemList = hotList
      }
    },

  }
};
</script>

<style lang="less" scoped>
</style>
